<?php
	include("datos.php");
	//session_name('usuario');
	//session_start();
	//$id_user=base64_encode($_SESSION['id_empleado']);
	//if($_SESSION['usuario'] == '' || $_SESSION['id_empleado'] == '')
	//	header("location:login.php?error=1");

	// Get the requested page. By default grid sets this to 1.
	$page = $_GET['page'];
	// get how many rows we want to have into the grid - rowNum parameter in the grid
	$limit = $_GET['rows'];
	// get index row - i.e. user click to sort. At first time sortname parameter -
	// after that the index from colModel
	$sidx = $_GET['sidx'];
	// sorting order - at first time sortorder
	$sord = $_GET['sord'];
	// if we not pass at first time index use the first column for the index or what you want
	if(!$sidx) $sidx =1;
		
	
	//****PARA BUSQUEDA POR TIPO *****//
	/*if(isset($_GET["tipobus"]))
		$tipobusqueda = $_GET['tipobus'];		
	if($tipobusqueda == "1"){
		$tipo = "idtag";
	elseif($tipobusqueda == "2"){
		
	}*/
		
	if(isset($_GET["cd_mask"]))
		$cd_mask = $_GET['cd_mask'];
	else $cd_mask = "";
	//construct where clause 
	$where = "";
	/*if($nm_mask!='')
		$where.= " AND Nombre LIKE '$nm_mask%'";*/
	if($cd_mask!='')
//		$where = "";
		$where.= " AND (e.nombre LIKE '%$cd_mask%' OR e.propietario LIKE '%$cd_mask%')";


		
	// the actual query for the grid data
	$consulta="SELECT e.id_establecimiento, e.nombre, e.renspa, c.apellido, e.propietario, e.superficie, e.cantidad_lotes, e.latitud, e.longitud FROM establecimiento e, cliente c where e.id_cliente=c.id_cliente;";
	
	// calculate the number of rows for the query. We need this for paging the result
	$count=$mi_objeto->contar($consulta);
	
	// calculate the total pages for the query
	if( $count > 0 && $limit > 0) {
		$total_pages = ceil($count/$limit);
	} else {
		$total_pages = 0;
	}
	
	// if for some reasons the requested page is greater than the total
	// set the requested page to total page
	if ($page > $total_pages) $page=$total_pages;
	
	// calculate the starting position of the rows
	$start = $limit*$page - $limit;
	
	// if for some reasons start position is negative set it to 0
	// typical case is that the user type 0 for the requested page
	if($start <0) $start = 0;

	$consulta="SELECT e.id_establecimiento,e.nombre, e.renspa, c.apellido, e.propietario, e.superficie, e.cantidad_lotes, e.latitud, e.longitud FROM establecimiento e, cliente c where e.id_cliente=c.id_cliente ".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
	
	$result=$mi_objeto->ejecutar_consulta($consulta);
	// we should set the appropriate header information. Do not forget this.
	//header("Content-type: text/xml;charset=utf-8");
	
	
	$responce->page = $page;
	$responce->total = $total_pages;
	$responce->records = $count;
	$i=0;
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		$responce->rows[$i]['id']=$row[id_establecimiento];
		$responce->rows[$i]['cell']=array("",$row[nombre],$row[renspa],$row[apellido],$row[propietario],$row[superficie],$row[cantidad_lotes],$row[latitud],$row[longitud]);
		$i++;
	}
	echo json_encode($responce);

?>